<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class BackAction extends BaseAction{
    
    function execute() {
        set_time_limit(0);
        parent::execute();
    }
    
    function _errorZh($obj){
        $array  = array('400 Bad Request'=>'', 'expired_token'=>'授权已过期');
        if(array_key_exists($obj, $array)){
            return $array[$obj];
        }
        return $obj;
    }
    

    /**
     *
     * @return DB_OAUTH 
     */
    function getOauth(){
        $where  = 'last_used=0';
        $oauth  = ORM_Finder::findOne('DB_OAUTH', $where);
        if(!$oauth){
            return false;
        }
        return $oauth;
    }
    
    /**
     * 获取微博ID
     * @return type 
     */
    protected function _get_param_json_weibo_id(){
        $weibo_id   = $this->params['weibo_id'];
        if(!$weibo_id){
            $this->_json_error('need weibo id');
        }
        return $weibo_id;
    }
    
    /**
     * 授权KEY
     * @return type 
     */
    protected function _get_param_json_token(){
        $token   = $this->params['token'];
        if(!$token){
            $this->_json_error('need token');
        }
        return $token;
    }
    /**
     * api异常捕获
     * @param type $obj
     * @return type 
     */
    protected function _exception($obj){
        if(array_key_exists('error', $obj) && array_key_exists('error_code', $obj)){
            $this->_json_error($this->_errorZh($obj['error']));
        }
        return $obj;
    }
    
    /**
     * 输出json错误
     * @param type $error 
     */
    protected function _json_error($error){
        echo json_encode(array('code'=>'error','data'=>$error));
        exit;
    }    
    
    /**
     * 输出json ok
     */
    protected function _json_ok($msg){
        echo json_encode(array('code'=>'ok','data'=>$msg));
        exit;
    }    
}
?>
